<!-- 首页 -->
<template>
	<view class="page_box">
		<!-- 空白页 -->
		<view class="page_box df-yc">
			<!-- 导航栏 -->
			<view class="head_box" :style="{ background: bgcolor }">
				<cu-custom backColor="#333333" :isBack="true" :showBackIcon="true">
					<block slot="backText">
						<text class="nav-title shopro-selector-rect">茶能成事</text>
					</block>
				</cu-custom>
			</view>
			<!-- 记录列表 -->
			<view class="scroll_wrap" style="overflow: hidden;">
				<scroll-view scroll-y="true" scroll-with-animation enable-back-to-top>
					<view class="history_items df-yc">
						<view style="color: #cccccc;" class="mt30" v-if="historyList.length==0">--暂无扫码记录--</view>
						<view class="item mt30 bs df-y" v-for="(item,index) in historyList" :key="index">
							<view class="top bb df-c">
								<view class="big_title titleColor fb fs36">{{item.title}}</view>
								<view class="score df-y">
									<view class="add fb highlight fs26">茶多酚+{{item.get_score}}</view>
								</view>
							</view>
							<view class="bottom fs26 lightColor bs df-c">
								<view class="left h100 df-y">
									<view class="id">唯一标识：{{item.code}}</view>
									<view class="open_time">开{{item.type=='box'?'盒':'罐'}}时间：{{item.createtime}}</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>

			<!-- 开罐按钮 -->
			<view @click="openScan" class="button df-c fs28">
				<image class="mr10" style="width: 26rpx;" :src="temImgUrl+'/scan.png'" mode="widthFix"></image>
				<text class="white">继续开罐</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapActions,
		mapState
	} from 'vuex';

	export default {
		components: {

		},
		data() {
			return {
				temImgUrl: this.$IMG_URL_TEMP,
				bgcolor: '#f6f6f6',
				teabox_id: 0,
				historyList: [],
				curPage: 1,
				type: 'one'
			};
		},
		onPullDownRefresh() {

		},
		onLoad(options) {
			this.type = this.$Route.query.type
			if (this.$Route.query.type == 'one') {
				this.getHistory(this.$Route.query.teabox_id)
			} else {
				this.getAllHistory()
			}

		},
		created() {

		},
		computed: {},
		methods: {
			// 路由跳转
			jump(path, params) {
				this.$Router.push({
					path: path,
					query: params
				});
			},
			openScan() {
				let that = this
				uni.scanCode({
					success: function(res) {
						that.scan(that.codeParse(res.result))
					}
				});
			},
			// url解析code
			codeParse(url) {
				let codeStr = url.split('?')[1]
				let code = codeStr.split('=')[1]
				return code
			},
			scan(code) {
				uni.showLoading({
					title: '添加中',
					icon: 'loading'
				})
				const that = this
				this.$api('tea.scanAdd', {
					code
				}).then(res => {
					if (res.code == 401)
						wx.showModal({
							title: '添加失败',
							content: res.msg,
							success: function(res) {
								if (res.confirm) {
									this.$store.commit('LOGIN_TIP', true)
								} else {
									console.log('用户点击取消')
								}

							}
						})
					if (res.code == 1) {
						uni.hideLoading()
						uni.showLoading({
							title: '添加成功',
							icon: 'success'
						})
						setTimeout(function() {
							that.jump("/pages/scan/scanSuccess/scanSuccess", {
								data: JSON.stringify(res.data),
								type: res.data.pro_type
							})
							uni.hideLoading()
						}, 1000);
					} else if (res.code == 0) {
						setTimeout(function() {
							uni.navigateBack()
						}, 1000);
					}
				})
			},
			getHistory(id) {
				this.$api('tea.openJarHistory', {
					teabox_id: parseInt(id)
				}).then(res => {
					console.log(res)
					this.historyList = res.data
				})
			},
			getAllHistory() {
				this.$api('tea.openHistory').then(res => {
					console.log(res)
					this.historyList = res.data.data
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.page_box {
		display: flex;
		flex-direction: column;
		height: 100vh;
	}


	.head_box {
		width: 100vw;

		.nav-title {
			font-size: 32rpx;
			font-family: PingFang SC;
			color: #333333;
		}
	}

	.scroll_wrap {
		height: 100%;

		scroll-view {
			height: 100%;
			//border: 1px solid blue;
		}
	}

	.history_items {
		padding-bottom: 180rpx;

		.item {
			padding: 0 30rpx;
			width: 690rpx;
			height: 283rpx;
			background: #FFFFFF;
			box-shadow: 0px 15rpx 59rpx 0px rgba(153, 153, 153, 0.1);
			border-radius: 20rpx;

			.top {
				height: 130rpx;
				justify-content: space-between;

				.score {
					align-items: flex-end;
				}
			}

			.bottom {
				flex: 1;
				padding: 34rpx 0;
				//background-color: pink;
				justify-content: space-between;

				.left {
					justify-content: space-between;
				}

				.right {
					align-items: flex-end;
				}
			}
		}
	}

	.button {
		width: 630rpx;
		height: 110rpx;
		background: linear-gradient(270deg, #53181E, #7D333B);
		box-shadow: 0px 5rpx 18rpx 0rpx rgba(83, 24, 30, 0.35);
		border-radius: 55rpx;
		position: fixed;
		bottom: 80rpx;
	}
</style>
